/**
 * 
 */
package pit.com.ordermobilesystem.imageutil;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Gallery;

/**
 * @author Dinh Hoang Son.
 * 
 */
public class FixGalleryScrollFast extends Gallery {
	private Runnable runDelay;
	private long scrollTime;
	private boolean doTouch;

	public FixGalleryScrollFast(Context context) {
		super(context);
	}

	public FixGalleryScrollFast(Context paramContext, AttributeSet paramAttributeSet) {
		super(paramContext, paramAttributeSet);
	}

	public FixGalleryScrollFast(Context paramContext, AttributeSet paramAttributeSet, int paramInt) {
		super(paramContext, paramAttributeSet, paramInt);
	}

	private void method_2765() {
		if (this.runDelay == null) {
			runDelay = new Runnable() {
				@Override
				public void run() {
					runDelay = null;
					method_2767();
				}
			};
			this.postDelayed(this.runDelay, 250L);
		}
	}

	private void method_2767() {
		if (System.currentTimeMillis() - this.scrollTime > 250L && !this.doTouch) {
			this.requestLayout();
		} else {
			this.method_2765();
		}
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		if (System.currentTimeMillis() - this.scrollTime > 250L && !this.doTouch) {
			super.onLayout(changed, l, t, r, b);
		} else {
			this.method_2765();
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == 0) {
			this.doTouch = true;
		} else if (event.getAction() == 1 || event.getAction() == 3) {
			this.doTouch = false;
		}
		return super.onTouchEvent(event);
	}

	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		super.onScrollChanged(l, t, oldl, oldt);
		this.scrollTime = System.currentTimeMillis();
	}
}
